import { EnumBjlResult } from "@/models/enums/EnumBjlResult";
import { ExcutePlan, RecmInfo, RoundInfo } from "../ExecutePlan";
import { FixedStrategy } from "../recmStrategy/FixedStrategy";
import { WinRabbitMoneyStrategyV2 } from "./WinRabbitMoneyStrategy";
import { IMoneyStrategy } from "../IMoneyStrategy";
import { IPlan } from "../IExecutePlan";
import { NormalGroupMoneyStrategy } from "./NormalGroupMoneyStrategy";
import { WisdomGroupMoneyStrategyV2 } from "./WisdomMoneyStrategyV2";
import { BossRabbitMoneyStrategy, MaxBossRabbitMoneyStrategy } from "./BossRabitMoneyStrategy";
import { WithRecmMoneyStrategy } from "./WithRecmMoneyStrategy";
import { MaxStepMoneyhStrategy, NormalStepMoneyStrategy } from "./NormalStepMoneyStrategy";
import { WisdomMoneyStrategyV29Group4 } from "./WisdomMoneyStrategyV29Group4";


export class WisdomMoneyStrategyV29 implements IMoneyStrategy {
    subPlan: IPlan[];
    debugCompoenent: string = "WisdomMoneyStrategyV29Debug";
    strategyName: string;
    startIndex: number = -1;
    startLevel: number = 10000;
    protected _subPlanSum: number = 0;
    public get subPlanSum() {
        return this._subPlanSum;
    }
    constructor(name?: string) {
        this.strategyName = name ?? "GroupMoneyStrategy" + (new Date()).getMilliseconds() + parseInt((Math.random() * 1000).toString());
        this.subPlan = [];
        this.resetStrategy();
    }
    showMoney(): [EnumBjlResult, number] {
        throw new Error("Method not implemented.");
    }
    resetRecm() {
        this.subPlan.forEach(item => item.resetRecm());
    }
    resetStrategy(): void {
        this._subPlanSum = 0;

        let flPlan = new ExcutePlan();
        flPlan.planName = "复利";
        flPlan.baseMoney = 10;
        flPlan.recmStrategy = [new FixedStrategy({ reverse: false })]
        flPlan.moneyStrategy = [new NormalGroupMoneyStrategy("", {
            groupCount: 1,
            addStep: 10,
            devideStep: -10,
            reverse: false,
            maxLevel: 1000000000
        })];
        flPlan.toResult(new RoundInfo());

        //正向，兔子算法
        let f_rabbit_plan = new ExcutePlan();
        f_rabbit_plan.planName = "正兔";
        f_rabbit_plan.recmStrategy = [new FixedStrategy({ reverse: false })];
        f_rabbit_plan.moneyStrategy = [new BossRabbitMoneyStrategy("方案一", {
            baseMoney: 10,
            winStep: 1,
            failStep: 0,
            currentMoney: 10,
            prevMoney: 10
        })];
        f_rabbit_plan.toResult(new RoundInfo());

        //反向，兔子算法
        let p_rabbit_plan = new ExcutePlan();
        p_rabbit_plan.planName = "反兔";
        p_rabbit_plan.recmStrategy = [new FixedStrategy({ reverse: true })]
        p_rabbit_plan.moneyStrategy = [new BossRabbitMoneyStrategy("方案二", {
            baseMoney: 10,
            winStep: 1,
            failStep: 0,
            currentMoney: 10,
            prevMoney: 10
        })];
        p_rabbit_plan.toResult(new RoundInfo());


        let tz = new ExcutePlan();
        tz.planName = "兔正";
        tz.recmStrategy = [new FixedStrategy({ reverse: false })];
        tz.moneyStrategy = [new BossRabbitMoneyStrategy("方案三", {
            baseMoney: 10,
            winStep: -3,
            failStep: 1,
            currentMoney: 10,
            prevMoney: 0,
            maxMoney: 2330,
        })];
        tz.toResult(new RoundInfo());


        let tf = new ExcutePlan();
        tf.planName = "兔反";
        tf.recmStrategy = [new FixedStrategy({ reverse: true })];
        tf.moneyStrategy = [new BossRabbitMoneyStrategy("方案三", {
            baseMoney: 10,
            winStep: -3,
            failStep: 1,
            currentMoney: 10,
            prevMoney: 0,
            maxMoney: 2330,
        })];
        tf.toResult(new RoundInfo());

        let v29group = new ExcutePlan();
        v29group.planName = "组合";
        v29group.recmStrategy = [new FixedStrategy({ reverse: false })]
        v29group.moneyStrategy = [new WisdomMoneyStrategyV29Group4("组合")];
        this.subPlan = [flPlan, f_rabbit_plan, p_rabbit_plan, tz, tf, v29group];
        //根据自己记录胜负数投注，红色正向投注，蓝色反向投注，0不投，

        // let flPlan = new ExcutePlan();
        // flPlan.planName = "复利";
        // flPlan.recmStrategy = [new FixedStrategy({ reverse: false })]
        // flPlan.moneyStrategy = [new NormalGroupMoneyStrategy("", {
        //     groupCount: 1,
        //     addStep: 100,
        //     devideStep: -100,
        //     reverse: false,
        //     maxLevel: 1000000000
        // })];
        // flPlan.toResult(new RoundInfo());

        // let array = [2];
        // this.subPlan = [f_rabbit_plan, p_rabbit_plan/*, flPlan*/];
        // for (let i = 0; i < array.length; i++) {
        //     let iv = array[i];
        //     let gi = new ExcutePlan();
        //     gi.planName = `正${iv}`;
        //     gi.baseMoney = 50;
        //     gi.recmStrategy = [new FixedStrategy({ reverse: false })];
        //     gi.relationPindex = [this.subPlan.length + 1];
        //     gi.moneyStrategy = [new WisdomGroupMoneyStrategyV2(`$wisdom-group-${i}`, {
        //         groupCount: (iv),
        //         addStep: 50,
        //         devideStep: -50,
        //         reverse: false, //true:赢了+100，输了-100
        //         maxLevel: 300000000,
        //     })]
        //     gi.toResult(new RoundInfo());
        //     this.subPlan.push(gi);

        //     let gif = new ExcutePlan();
        //     gif.planName = `反${iv}`;
        //     gif.baseMoney = 50;
        //     gif.relationPindex = [this.subPlan.length - 1];
        //     gif.recmStrategy = [new FixedStrategy({ reverse: true })];
        //     gif.moneyStrategy = [new WisdomGroupMoneyStrategyV2(`$reverse-wisdom-group-${i}`, {
        //         groupCount: (iv),
        //         addStep: 50,
        //         devideStep: -50,
        //         reverse: false, //true:赢了+100，输了-100
        //         maxLevel: 300000000
        //     })]
        //     gif.toResult(new RoundInfo());
        //     this.subPlan.push(gif);

        // }
        // var pgPlan1 = new ExcutePlan();
        // pgPlan1.planName = "复正";
        // pgPlan1.recmStrategy = [new FixedStrategy({ reverse: false })];
        // pgPlan1.moneyStrategy = [new NormalStepMoneyStrategy("方案一", {
        //     baseMoney: 10,
        //     startMoney: 10,
        //     addStep: 10,
        //     devideStep: -10,
        //     reverse: false,
        //     resetLevel: 80,
        // }, true)];
        // pgPlan1.toResult(new RoundInfo());
        // this.subPlan.push(pgPlan1);

        // var pgfPlan1 = new ExcutePlan();
        // pgfPlan1.planName = "复反";
        // pgfPlan1.recmStrategy = [new FixedStrategy({ reverse: true })];
        // pgfPlan1.moneyStrategy = [new NormalStepMoneyStrategy("方案二", {
        //     baseMoney: 10,
        //     startMoney: 10,
        //     addStep: 10,
        //     devideStep: -10,
        //     reverse: false,
        //     resetLevel: 80,
        // }, true)];
        // pgfPlan1.toResult(new RoundInfo());
        // this.subPlan.push(pgfPlan1);

    }
    toMoney(roundInfo: RoundInfo, recmInfo: RecmInfo, userRecm: RecmInfo, baseMoney: number): [EnumBjlResult, number] {
        let zm = 0;
        let xm = 0;
        let recmV = EnumBjlResult.U;
        let recmMoney = 0;
        let sumWin = 0;
        //let groupWin = 0;
        // this.subPlan.forEach((pl, index) => {

        //     // if (pl.planName == "正2") {
        //     //     debugger;
        //     // }
        //     // else if (pl.planName == "反2") {
        //     //     debugger;
        //     // }

        //     pl.setResult(roundInfo, pl.recmInfo, pl.multiple, pl.waterRate, pl.fuliRate);
        //     let userRecmV = pl.toResult(roundInfo);
        //     let newRoundInfo: RoundInfo = Object.assign({}, roundInfo, { userRecm: userRecmV });
        //     pl.userRecmInfo.recmV = userRecmV;
        //     let win = pl.recmInfo.summaryInfo.winMoney - pl.recmInfo.summaryInfo.failMoney;
        //     sumWin += win;

        //     // if (index > 2) {
        //     //     groupWin += win;
        //     // }
        //     if (index > 2 && index < 4 && index % 2 == 1) {
        //         let [rv, rm] = pl.toMoney(newRoundInfo, pl.recmInfo, pl.userRecmInfo, pl.baseMoney);
        //         pl.recmInfo.preBetCount = pl.recmInfo.betCount;
        //         pl.recmInfo.recmV = rv;
        //         pl.recmInfo.recmMoney = rm;
        //         if (rv != EnumBjlResult.U) {
        //             //pl.recmInfo.
        //             pl.recmInfo.betCount++;
        //         }

        //         let giPlan = this.subPlan[index - 1] as ExcutePlan;
        //         let gifPlan = this.subPlan[index] as ExcutePlan;

        //         let giMoneyStrategy = giPlan.moneyStrategy[0] as WisdomGroupMoneyStrategyV2;
        //         let gifMoneyStrategy = gifPlan.moneyStrategy[0] as WisdomGroupMoneyStrategyV2;

        //         let giStartFlag = giMoneyStrategy && giMoneyStrategy.currentRecmMoney >= this.startLevel;
        //         let gifStartFlag = gifMoneyStrategy && gifMoneyStrategy.currentRecmMoney >= this.startLevel;
        //         //nrmMoneyStrategy.gameStart = true;
        //         if (giStartFlag) {
        //             gifMoneyStrategy.stop();
        //             gifPlan.recmInfo.recmV = EnumBjlResult.U;
        //         }
        //         else if (gifStartFlag) {
        //             giMoneyStrategy.stop();
        //             giPlan.recmInfo.recmV = EnumBjlResult.U;
        //         }

        //         if (giMoneyStrategy.start && !gifMoneyStrategy.start) {
        //             let sum = giMoneyStrategy.getSum();
        //             if (sum == 0) {
        //                 giPlan.resetPlan();
        //                 gifPlan.resetPlan();
        //             }
        //         }
        //         else if (gifMoneyStrategy.start && !giMoneyStrategy.start) {
        //             let sum = gifMoneyStrategy.getSum();
        //             if (sum == 0) {
        //                 giPlan.resetPlan();
        //                 gifPlan.resetPlan();
        //             }
        //         }

        //     }
        //     else {
        //         let [rv, rm] = pl.toMoney(newRoundInfo, pl.recmInfo, userRecm, pl.baseMoney);
        //         pl.recmInfo.preBetCount = pl.recmInfo.betCount;
        //         pl.recmInfo.recmV = rv;
        //         pl.recmInfo.recmMoney = rm;
        //         if (rv != EnumBjlResult.U) {
        //             //pl.recmInfo.
        //             pl.recmInfo.betCount++;
        //         }
        //     }

        // });
        // //let fl_plan = this.subPlan[2];
        // //let flwin = fl_plan.recmInfo.summaryInfo.winMoney - fl_plan.recmInfo.summaryInfo.failMoney;
        // let reset = false;
        // this._subPlanSum = sumWin;
        // // if((groupWin - flwin) > 300 || (flwin - groupWin )> 3000){
        // //     reset = true;
        // // }
        // // let flFlag = flwin > 0 ? (flwin - Math.abs(groupWin)) > 300 : false;
        // // let groupFlag = groupWin > 0 ? (groupWin - Math.abs(flwin) > 300) : false;
        // // if(flFlag || groupFlag){
        // //     reset = true;
        // // }

        // if (roundInfo.changeCount > 0 && reset) {
        //     this.subPlan.forEach((pl, index) => {
        //         let plan = pl as ExcutePlan;
        //         if (plan && index > 1) {
        //             plan.resetPlan();
        //             let userRecmV = plan.toResult(roundInfo);
        //             let newRoundInfo: RoundInfo = Object.assign({}, roundInfo, { userRecm: userRecmV });
        //             pl.userRecmInfo.recmV = userRecmV;

        //             let [rv, rm] = plan.toMoney(newRoundInfo, recmInfo, userRecm, pl.baseMoney);
        //             pl.recmInfo.preBetCount = pl.recmInfo.betCount;
        //             pl.recmInfo.recmV = rv;
        //             pl.recmInfo.recmMoney = rm;
        //         }
        //     });
        // }
        // this.subPlan.forEach((pl, index) => {
        //     pl.recmInfo.recmV = pl.recmInfo.recmV;
        //     pl.recmInfo.recmMoney = pl.recmInfo.recmMoney;

        //     if (pl.recmInfo.recmV == EnumBjlResult.Z) {
        //         zm += pl.recmInfo.recmMoney;

        //     }
        //     else if (pl.recmInfo.recmV == EnumBjlResult.X) {
        //         xm += pl.recmInfo.recmMoney;
        //     }
        // });


        this.subPlan.forEach(pl => {
            pl.setResult(roundInfo, pl.recmInfo, pl.multiple, pl.waterRate, pl.fuliRate);
            let userRecmV = pl.toResult(roundInfo);
            let newRoundInfo: RoundInfo = Object.assign({}, roundInfo, { userRecm: userRecmV });
            pl.userRecmInfo.recmV = userRecmV;
            let [rv, rm] = pl.toMoney(newRoundInfo, pl.recmInfo, userRecm, pl.baseMoney);
            pl.recmInfo.preBetCount = pl.recmInfo.betCount;
            pl.recmInfo.recmV = rv;
            pl.recmInfo.recmMoney = rm;
            if (pl.recmInfo.recmV == EnumBjlResult.Z) {
                zm += pl.recmInfo.recmMoney;

            }
            else if (pl.recmInfo.recmV == EnumBjlResult.X) {
                xm += pl.recmInfo.recmMoney;
            }
            if (rv != EnumBjlResult.U) {
                //pl.recmInfo.
                pl.recmInfo.betCount++;
            }
        });
        if (zm > xm) {
            recmV = EnumBjlResult.Z;
            recmMoney = zm - xm;
        }
        else if (xm > zm) {
            recmV = EnumBjlResult.X;
            recmMoney = xm - zm;
        }
        else {
            recmV = EnumBjlResult.U;
        }


        return [recmV, recmMoney];
    }

    copyValue(valueObj: any): void {
        var obj = valueObj as WisdomMoneyStrategyV29;
        if (obj) {
            this.debugCompoenent = obj.debugCompoenent;
            this.startIndex = obj.startIndex;
            this._subPlanSum = obj._subPlanSum;
            for (let i = 0; i < this.subPlan.length; i++) {
                this.subPlan[i].copyValue(obj.subPlan[i]);
            }
        }
    }
}